/*
 * GPS_MTK.h
 *
 * Created: 01.05.2012 13:24:49
 *  Author: maxim_karban@gmail.com
 */ 


#ifndef GPS_H_
#define GPS_H_

#include <stdbool.h>
#include <stdint.h>

#define GPS_PARSER_BUF_SIZE 15

#define GPS_DELAY _delay_ms(700)

#define GPS_RESET_SETUP DDRC |= 1 << 1
#define GPS_RESET_IN    DDRC &= ~(1 << 1)
#define GPS_RESET_ON    PORTC |= 1 << 1
#define GPS_RESET_OFF   PORTC &= ~(1 << 1)

#define GPS_ON_SETUP    DDRB |= 1 << PB2
#define GPS_ON_IN       DDRB &= ~(1 << PB2)
#define GPS_ON          PORTB |= (1 << PB2)
#define GPS_OFF         PORTB &= ~(1 << PB2)

#define SCK_SETUP       DDRB |= 1 << 5
#define SCK_IN          DDRB &= ~(1 << 5)
#define SCK_ON          PORTB |= 1 << 5
#define SCK_OFF         PORTB &= ~(1 << 5)

#define GPSCS_SETUP     DDRD |= 1 << 2
#define GPSCS_IN        DDRD &= ~(1 << 2)
#define GPSCS_ON        PORTD |= 1 << 2
#define GPSCS_OFF       PORTD &= ~(1 << 2)

#define GPS_PIN_SETUP   GPS_ON_SETUP; GPS_RESET_SETUP; GPSCS_SETUP; SCK_SETUP
#define GPS_PIN_IN      GPS_ON_IN; GPS_RESET_IN; GPSCS_IN; SCK_IN



#define gps_sentences_is_ready \
  ( hLock.gps_gga_is_ready & \
    hLock.gps_rmc_is_ready )

enum SMState_t{
  SM_START,
  SM_L_G0,
  SM_L_P0,

  SM_L_R0,
  SM_L_M0,
  SM_L_C0,

  SM_L_G1,
  SM_L_G2,
  SM_L_A0,

  SM_L_S0,
  SM_L_A1,

  SM_L_V0,

  SM_L_P1,
  SM_L_S1,
  SM_L_R1,
  SM_L_F0,
  SM_L_10,
  SM_L_50,
  SM_L_00,
  SM_L_60,
  SM_PCRF150_LF,
  SM_PCRF156_LF,

  SM_RMC_COMA,
  SM_RMC_TIME,
  SM_RMC_STATUS,
  SM_RMC_LAT,
  SM_RMC_LAT_NS,
  SM_RMC_LON,
  SM_RMC_LON_EW,
  SM_RMC_SPEED,
  SM_RMC_DIR,
  SM_RMC_DATE,
  SM_RMC_MAG,
  SM_RMC_MAG_EW,
  SM_RMC_MODE,
  SM_RMC_CRC,
  SM_RMC_LF,
  SM_GGA_COMA,
  SM_GGA_TIME,
  SM_GGA_LAT,
  SM_GGA_LAT_NS,
  SM_GGA_LON,
  SM_GGA_LON_EW,
  SM_GGA_Q,
  SM_GGA_CNT,
  SM_GGA_HDOP,
  SM_GGA_ALT,
  SM_GGA_ALT_M,
  SM_GGA_GEO,
  SM_GGA_GEO_M,
  SM_GGA_DIF,
  SM_GGA_DIF_ID,
  SM_GGA_CRC,
  SM_GGA_LF,
  
  SM_ANY_LF,
  
  SM_IDLE_A70,
  SM_IDLE_B40,
  
  SM_IDLE,
  SM_ERROR,
  SM_CRC_ERROR,
  SM_OK,
  SM_STOP,
  SM_LAST
};

struct gpsNavData_t{
  uint8_t status;
  int16_t alt_bp;
  uint16_t alt_ap;
  uint8_t alt_measure;
  int16_t lat_bp;
  uint16_t lat_ap;
  uint8_t lat_ns;
  int16_t lon_bp;
  uint16_t lon_ap;
  uint8_t lon_ew;
  int16_t speed_bp;
  uint16_t speed_ap;
  int16_t course_bp;
  uint16_t course_ap;
  int16_t cnt;
  uint16_t cnt_ap;

  uint16_t date_day_month;
  uint16_t date_year;
  uint16_t time_hour_min;
  uint16_t time_sec;
};

struct gpsData_t{
  gpsNavData_t gpsNavData;
  gpsNavData_t gpsNavDataCache;
  
  bool crc_is_ok;
  
  uint8_t crc_pos;
  
  char parser_buff[GPS_PARSER_BUF_SIZE];
};


extern gpsData_t gpsData;

extern void gpsInit(void);
extern void strtobpap(uint8_t * _str, int16_t *bp, uint16_t *ap);



#endif /* GPS_H_ */